﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharpWinformDemo;
using CefSharp;

namespace CefSharpWinformDemo
{
    public partial class BrowserDemoCtrl : MapCtrl, IDisposable
    {
        public BrowserCtrl browserCtrl;

        public BrowserDemoCtrl(string url = null)
        {
            InitializeComponent();

            txtUrl.Text = url;

            browserCtrl = new BrowserCtrl();
            browserCtrl.Dock = DockStyle.Fill;

            //C#和JS互操作
            JSObject jsObject = new JSObject();
            jsObject.MapCtrl = this;
            browserCtrl.RegisterJsObject("jsObj", jsObject);

            browserCtrl.Browser.TitleChanged += (s, e) =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    TabPage tabItem = this.Parent as TabPage;
                    string title = e.Title;
                    if (title.Length > 20) title = title.Substring(0, 20) + "...";
                    tabItem.Text = title;
                    tabItem.BringToFront();
                }));
            };
            browserCtrl.Browser.FrameLoadStart += (s, e) =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    txtUrl.Text = string.IsNullOrWhiteSpace(browserCtrl.Browser.Address) ? e.Url : browserCtrl.Browser.Address;
                }));
            };
            browserCtrl.Browser.FrameLoadEnd += (s, e) =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    txtUrl.Text = string.IsNullOrWhiteSpace(browserCtrl.Browser.Address) ? e.Url : browserCtrl.Browser.Address;
                }));
            };
            this.splitContainer1.Panel2.Controls.Add(browserCtrl);
        }

        private void BrowserDemoCtrl_Load(object sender, EventArgs ee)
        {

        }

        private void txtUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                browserCtrl.LoadUrl(txtUrl.Text.Trim());
            }
        }

        #region Dispose 释放资源
        /// <summary>
        /// 释放资源
        /// </summary>
        public new void Dispose()
        {
            browserCtrl.Dispose();
            base.Dispose();
        }
        #endregion

        #region 实现 IMapCtrl 接口

        /// <summary>
        /// 地图加载完毕
        /// </summary>
        public override void MapLoaded()
        {
            TestCallJs();
        }

        /// <summary>
        /// 测试JS调用C#方法
        /// </summary>
        public override void TestCallCSharp(string data)
        {
            MessageBox.Show("测试JS调用C#方法：" + data);
        }

        #endregion 实现 IMapCtrl 接口

        #region 测试调用JS方法
        /// <summary>
        /// 测试调用JS方法
        /// </summary>
        private void TestCallJs()
        {
            browserCtrl.Browser.ExecuteScriptAsync("testCallJs", "测试参数789");
        }
        #endregion

    }
}
